package com.glc.hms.core.compress;

import com.glc.hms.core.compress.algorithm.CompressAlgorithm;
import com.glc.hms.core.compress.algorithm.GZIPCompressAlgorithm;
import com.glc.hms.core.compress.algorithm.LZ4CompressAlgorithm;
import com.glc.hms.core.compress.algorithm.SnappyCompressAlgorithm;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author Lin
 * @time 2019-12-04 22:27
 */
public enum CompressUtil {

    GZIP {
        CompressAlgorithm compressAlgorithm = new GZIPCompressAlgorithm();

        @Override
        public byte[] compress(byte[] data) {
            return compressAlgorithm.compress(data);
        }

        @Override
        public byte[] decompress(byte[] data) {
            return compressAlgorithm.decompress(data);
        }

        @Override
        public byte[] compressString(String data) {
            if (StringUtils.isNotBlank(data)) {
                return compressAlgorithm.compress(getBytes(data));
            } else {
                return new byte[0];
            }
        }

        @Override
        public String decompressString(byte[] data) {
            if (data == null || data.length == 0) {
                return null;
            }
            return new String(compressAlgorithm.decompress(data), StandardCharsets.UTF_8);
        }
    },

    LZ4 {
        CompressAlgorithm compressAlgorithm = new LZ4CompressAlgorithm();

        @Override
        public byte[] compress(byte[] data) {
            return compressAlgorithm.compress(data);
        }

        @Override
        public byte[] decompress(byte[] data) {
            return compressAlgorithm.decompress(data);
        }

        @Override
        public byte[] compressString(String data) {
            if (StringUtils.isNotBlank(data)) {
                return compressAlgorithm.compress(getBytes(data));
            } else {
                return new byte[0];
            }
        }

        @Override
        public String decompressString(byte[] data) {
            if (data == null || data.length == 0) {
                return null;
            }
            return new String(compressAlgorithm.decompress(data), StandardCharsets.UTF_8);
        }
    },

    SNAPPY {
        CompressAlgorithm compressAlgorithm = new SnappyCompressAlgorithm();

        @Override
        public byte[] compress(byte[] data) {
            return compressAlgorithm.compress(data);
        }

        @Override
        public byte[] decompress(byte[] data) {
            return compressAlgorithm.decompress(data);
        }

        @Override
        public byte[] compressString(String data) {
            if (StringUtils.isNotBlank(data)) {
                return compressAlgorithm.compress(getBytes(data));
            } else {
                return new byte[0];
            }
        }

        @Override
        public String decompressString(byte[] data) {
            if (data == null || data.length == 0) {
                return null;
            }
            return new String(compressAlgorithm.decompress(data), StandardCharsets.UTF_8);
        }
    };

    public byte[] compress(byte[] data) {
        throw new AbstractMethodError();
    }

    public byte[] decompress(byte[] data) {
        throw new AbstractMethodError();
    }

    public byte[] compressString(String data) {
        throw new AbstractMethodError();
    }

    public String decompressString(byte[] data) {
        throw new AbstractMethodError();
    }

    public static byte[] getBytes(String data) {
        if (data == null) {
            throw new NullPointerException();
        }
        return data.getBytes(StandardCharsets.UTF_8);
    }

}
